package server

import (
  "encoding/json"
  "io/ioutil"
)

type dbConfig struct {
  Host      string `json:"host"`
  User      string `json:"user"`
  Passwd    string `json:"passwd"`
  DbName    string `json:"db_name"`
  DbMaxConn int    `json:"db_maxconn"`
}

type Config struct {
  Addr string `json:"addr"`

  HttpAddr string `json:"http_addr"`

  DataDir string `json:"data_dir"`

  AccessLog string `json:"access_log"`

  MysqlConf dbConfig `json:"mysqldb"`
}

func NewConfig(data json.RawMessage) (*Config, error) {
  c := new(Config)

  err := json.Unmarshal(data, c)
  if err != nil {
    return nil, err
  }

  return c, nil
}

func NewConfigWithFile(fileName string) (*Config, error) {
  data, err := ioutil.ReadFile(fileName)
  if err != nil {
    return nil, err
  }

  return NewConfig(data)
}
